//index.js
var app = getApp()
Page({
  data: {
    photoAlbum: [],
    shareBtn: false,
    isshare: 0,
    getInfo: 1,
  },
  createAlbum: function (data){
    var _this = this;
    var open_id = wx.getStorageSync('open_id');
    app.request.requestPostApi({
      url: app.apiHost + 'album/create-album',
      params: {
        iv: data.iv,
        encryptedData: data.encryptedData,
        open_id
      },
      success: function (response) {
        if(response.code == 0){
          wx.navigateTo({
            url: '/pages/photos/photos?is_inner=1&id=' + response.data,
          })
        }else{
          _this.createAlbum(data);
        }
      }
    })
  },
  
  onShow: function(){
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 未授权 申请授权
          this.setData({
            getInfo: 0
          })
        }
      }
    })
    var _this = this;
    wx.showShareMenu({
      withShareTicket: true
    })
    var obj = app.globalData.obj;
    if (obj.query != {} && app.globalData.auto_go == 1) {
      if (obj.query.is_new == 1) {
        if (obj.shareTicket) {
          wx.getShareInfo({
            shareTicket: obj.shareTicket,
            success: function (res) {
              _this.createAlbum(res);
            }
          })
        }
      } else if (obj.query.id) {
        wx.navigateTo({
          url: '/pages/photos/photos?is_inner=1&id=' + obj.query.id,
        })
      }
      app.globalData.auto_go = 0;
    }
    this.getList();
    wx.hideLoading();
  },
  getUserInfo: function(res){
    app.userInfoReadyCallback(res.detail);
    this.setData({
      getInfo: 0
    })
  },
  getList: function(){
    var open_id = wx.getStorageSync('open_id');
    var url = app.apiHost + 'album/list';
    var _this = this;
    var params = {
      open_id
    }
    app.request.requestPostApi({
      url,
      params,
      success: function (res) {
        if (res.code == 0) {
          _this.setData({
            photoAlbum: res.data
          })
        }else{
          _this.getList();
        }
      }
    })
  },
  onShareAppMessage: function (res) {
    var _this = this;
    var path = 'pages/index/index?is_new=0';
    if (res.from === 'button') {
      // 来自页面内转发按钮
      _this.data.shareBtn = true;
      path = 'pages/index/index?is_new=1';
    } else {
      //来自右上角转发
      _this.data.shareBtn = false;
    }
    wx.showModal({
      content: '新的群相册已经创建，请从群内点击小程序进入。',
      showCancel: false,
      confirmText: '好的'
    })
    return {
      title: '邀请您加入本群相册',
      path,
      imageUrl: "/images/1.jpg",
      success: function (res) {
        console.log(res)
      },
      complete: function (res) {
        if (res.errMsg == 'shareAppMessage:ok') {
          //分享为按钮转发
          if (_this.data.shareBtn) {
            //判断是否分享到群
            if (res.hasOwnProperty('shareTickets')) {
              console.log(res.shareTickets[0]);
              //分享到群
              _this.data.isshare = 1;
            } else {
              // 分享到个人
              _this.data.isshare = 0;
            }
          }
        } else {
          wx.showToast({
            title: '分享失败',
          })
          _this.data.isshare = 0;
        }
      },
    }
  },
  viewPhoto:function(event){
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/photos/photos?is_inner=1&id='+id,
    })
  },

});